//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.滑动窗口.定长滑动窗口;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class demo_2841 {
    public demo_2841() {
    }

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList();
        nums.add(1);
        nums.add(2);
        nums.add(2);
        System.out.println(maxSum(nums, 2, 2));
    }

    public static long maxSum(List<Integer> nums, int m, int k) {
        Map<Integer, Integer> cnt = new HashMap();
        long sum = 0L;
        long max = 0L;

        for(int i = 0; i < nums.size(); ++i) {
            int num = (Integer)nums.get(i);
            cnt.merge(num, 1, Integer::sum);
            sum += (long)num;
            if (i >= k - 1) {
                if (cnt.size() >= m) {
                    max = Math.max(max, sum);
                }

                int out = (Integer)nums.get(i - k + 1);
                if ((Integer)cnt.merge(out, -1, Integer::sum) == 0) {
                    cnt.remove(out);
                }

                sum -= (long)out;
            }
        }

        return max;
    }
}
